package com.oniontech.stamptour.common.Task;

import android.database.Cursor;
import android.os.AsyncTask;

import com.oniontech.stamptour.common.adapter.Bean.ContentsListBean;
import com.oniontech.stamptour.common.database.DBContext;
import com.oniontech.stamptour.common.database.DatabaseManager;
import com.oniontech.stamptour.fragment.listener.QueryListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by limsy on 14. 1. 28.
 */
public class ContentsListQuery extends AsyncTask<Object, Object, List<ContentsListBean>> implements DBContext {
    private QueryListener listener = null;
    private String themeCode = null;
    private String contId = null;
    protected DatabaseManager m_DBMgr = null;

    public ContentsListQuery(String themeCode, String contId, QueryListener listener) {
        this.listener = listener;
        this.themeCode = themeCode;
        this.contId =contId;
        this.m_DBMgr = DatabaseManager.getInstance();

    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected List<ContentsListBean> doInBackground(Object... objects) {
        return getContentsListQuery(themeCode);
    }

    @Override
    protected void onPostExecute(List<ContentsListBean> o) {
        super.onPostExecute(o);
        listener.queryComplete(o);
    }

    public List<ContentsListBean> getContentsListQuery(String themeCode) {
        m_DBMgr.openPointDB(DB_TYPE_ADDRESS, true);

        String str = null;

        StringBuffer sb = new StringBuffer();

        sb.append("select\n");
        sb.append("theme_code,id,c.name,lon,lat,rad,url,visit,img,case when visit = 0 then listFrameOff else listFrameOn end as listFrameImg,case when visit = 0 then listImgOff else listImgOn end as listVistImg,case when visit = 0 then infoVisitImgOff else infoVisitImgOn end as infoVisitImg,t.name as themename \n");
        sb.append("from\n");
        sb.append("contents c inner join theme t on c.theme_code = t.code\n");
        sb.append("where\n");
        sb.append("c.theme_code = '"+ themeCode + "'\n");
        if(contId != null){
            sb.append("and c.id = '"+ contId + "'\n");
        }
        sb.append("order by c.oder\n");

        Cursor cursor = m_DBMgr.DBQuery(sb.toString());

        if (cursor == null || !cursor.moveToFirst())
        {
            m_DBMgr.closeDB();
            return null;
        }

        int cnt = cursor.getCount();
        List<ContentsListBean> contentsListBeanList = new ArrayList<ContentsListBean>();
        ContentsListBean contentsListBean = null;
        for (int i = 0; i < cnt; i++) {
            contentsListBean = new ContentsListBean();
            contentsListBean.setTheme_code(cursor.getString(cursor.getColumnIndex("theme_code")));
            contentsListBean.setId(cursor.getString(cursor.getColumnIndex("id")));
            contentsListBean.setName(cursor.getString(cursor.getColumnIndex("name")));
            contentsListBean.setLon(cursor.getFloat(cursor.getColumnIndex("lon")));
            contentsListBean.setLat(cursor.getFloat(cursor.getColumnIndex("lat")));
            contentsListBean.setRad(cursor.getFloat(cursor.getColumnIndex("rad")));
            contentsListBean.setUrl(cursor.getString(cursor.getColumnIndex("url")));
            contentsListBean.setVisit(cursor.getInt(cursor.getColumnIndex("url")));
            contentsListBean.setImg(cursor.getString(cursor.getColumnIndex("img")));
            contentsListBean.setListFrameImg(cursor.getString(cursor.getColumnIndex("listFrameImg")));
            contentsListBean.setListVisitImg(cursor.getString(cursor.getColumnIndex("listVistImg")));
            contentsListBean.setInfoVisitImg(cursor.getString(cursor.getColumnIndex("infoVisitImg")));
            contentsListBean.setThemeName(cursor.getString(cursor.getColumnIndex("themename")));

            contentsListBeanList.add(contentsListBean);
            cursor.moveToNext();
        }

        cursor.close();
        m_DBMgr.closeDB();


        return contentsListBeanList;
    }
}